#include "ZxPLService.h"


ZxPLService::ZxPLService(QObject* parent)
    : QxPLService("zigbus", "gateway", true, parent)
{
    initFilters();
    initConfigValues();
}

void ZxPLService::initConfigValues()
{
    addConfigValue("zigbus", QxPLConfigValue::OPTION, "disable");
    addConfigValue("media", QxPLConfigValue::OPTION, ""); // udp, rs232
    addConfigValue("port", QxPLConfigValue::OPTION, ""); // udp port, dev file path

    // RS232 args
    addConfigValue("bauds", QxPLConfigValue::OPTION, "");
    addConfigValue("data", QxPLConfigValue::OPTION, "");
    addConfigValue("flow", QxPLConfigValue::OPTION, "");
    addConfigValue("parity", QxPLConfigValue::OPTION, "");
    addConfigValue("stop", QxPLConfigValue::OPTION, "");
}

void ZxPLService::initFilters()
{
    filters().clear();

    //permet de recevoir les cmd config.response
    addFilter("xpl-cmnd.*.*.*.config.response");

    //permet de recevoir les cmd config.list:
    addFilter("xpl-cmnd.*.*.*.config.list");

    //permet de recevoir les config.current
    addFilter("xpl-cmnd.*.*.*.config.current");

    //Pour les modules
    addFilter("xpl-cmnd.*.*.*.control.basic");
}

void ZxPLService::processPendingMessage(const QxPLMessage &message)
{
    if(message.getTarget().getVendorId() == "zigbus"
            && message.getTarget().getDeviceId() == "module") {
        emit this->moduleTargetedMessage(message);
        return;
    }
    QxPLService::processPendingMessage(message);
}


bool ZxPLService::processConfigurationMessage(const QxPLMessage &message)
{
    if(!QxPLService::processConfigurationMessage(message))
        return false;

    if(message.getNamedValue("media") == "udp") {
        bool conversionOK = false;
        int port = message.getNamedValue("port").toInt(&conversionOK);
        if(!conversionOK) {
            qDebug() << "Cannot apply zigbus config udp on port \""+message.getNamedValue("port")+"\"";
            return false;
        }
        emit configZigbusMediaUdp(port,
                                  (message.getNamedValue("zigbus") == "enable"));
    }
    else if(message.getNamedValue("media") == "rs232") {
        QString port = message.getNamedValue("port");
        QString baud = message.getNamedValue("bauds");
        QString data = message.getNamedValue("data");
        QString flow = message.getNamedValue("flow");
        QString parity = message.getNamedValue("parity");
        QString stop = message.getNamedValue("stop");
        emit this->configZigbusMediaRS232(port, baud, data, flow, parity, stop,
                                          (message.getNamedValue("zigbus") == "enable"));
    }
    else if(message.getNamedValue("media").isEmpty()) {
        if(message.getNamedValue("zigbus") == "enable") {
            emit enableZigbus(true);
        }
        else if(message.getNamedValue("zigbus") == "disable") {
            emit enableZigbus(false);
        }
        else if(message.getNamedValue("zigbus") == "reload") {
            emit reloadZigbus();
        }
    }
    return true;
}

void ZxPLService::processMessage(QxPLMessage message)
{
    Q_UNUSED(message)
    qDebug() << "The gateway received an xPL message to process !";
}
